using System.Collections;
using System.Collections.Generic;
using Cysharp.Threading.Tasks;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
[UIAttr("MainScripts")]
public class MainScripts : PanelSingleton<MainScripts>
{
    [SerializeField]
    GameObject Content;
    [SerializeField]
    GameObject DisplayNode;
    Button InterBtn;
    Button MergeBtn;
    Button DiffBtn;
    Button AddBtn;
    private void Start()
    {
        InitListener();
    }
    protected override void Awake()
    {
        base.Awake();
        Content = FindNode(gameObject, "Content");
        DisplayNode = FindNode(gameObject, "DisplayNode");
        InterBtn = FindNode(gameObject, "Intersection").GetComponent<Button>();
        MergeBtn = FindNode(gameObject, "Merge").GetComponent<Button>();
        InterBtn = FindNode(gameObject, "Difference").GetComponent<Button>();
        AddBtn = FindNode(gameObject, "AddBtn").GetComponent<Button>();
        MergeBtn.onClick.AddListener(MergeEvent);
        Open();
    }

    void MergeEvent()
    {
        Debug.Log("MergeEvent");
        List<SelectData> merges = new List<SelectData>();
        foreach (Node selectData in nodeList)
        {
            if (selectData.toggle)
            {
                merges.Add(selectData.Data);
            }
        }

        resoult = MergeData(merges);
        
        ShowBallPanel.Instance.Open();
    }
    public static List<Ball> MergeData(List<SelectData> selectDatas)
    {
        List<Ball> all = new List<Ball>();

        for (int i = 0; i < selectDatas.Count; i++)
        {
            List<Ball> bs = MathScripts.ConvertBall(selectDatas[i]);

            for (int j = 0; j < bs.Count; j++)
            {
                if (!all.Exists((e) => { return MathScripts.HaveIsBall(e, bs[j]); }))
                {
                    all.Add(bs[j]);
                }
            }
        }

        return all;
    }
    void InitListener()
    {
        AddBtn.onClick.AddListener(() =>
        {
            StartCoroutine(OnAddBtn());
        });
    }
    public List<SelectData> DrawData = new List<SelectData>();
    IEnumerator OnAddBtn()
    {
        SelectPanel.Instance.Open();
        yield return new WaitUntil(() => { return SelectPanel.IsShow == false; });
        Debug.Log(SelectPanel.CurrentData);
        if (SelectPanel.CurrentData != null)
        {
            AddData(SelectPanel.CurrentData);
            DrawData.Add(SelectPanel.CurrentData);
        }
        
    }
    public List<Node> nodeList = new List<Node>();
    void AddData(SelectData data)
    {
        var obj = GameObject.Instantiate(DisplayNode, Content.transform);
        nodeList.Add(new Node(obj,data));
        obj.SetActive(true);
        // Content.transform
    }

    public class Node
    {
        public Toggle toggle;
        public TextMeshProUGUI text;
        SelectData _data;
        private Button changeBtn;

        public SelectData Data
        {
            get
            {
                return _data;
            }
        }
        public bool IsOn
        {
            get{return toggle.isOn;}
        }
        public Node(GameObject root,SelectData data)
        {
            _data = data;
            changeBtn = MainScripts.Instance.FindNode(root,"Change").GetComponent<Button>();
            toggle = MainScripts.Instance.FindNode(root,"Toggle").GetComponent<Toggle>();
            text = MainScripts.Instance.FindNode(root,"desc").GetComponent<TextMeshProUGUI>();
            toggle.isOn = true;
            InitUI();
            changeBtn.onClick.AddListener(() =>
            {
                MainScripts.Instance.StartCoroutine(ResaveData());
            });
        }

        void InitUI()
        {
            text.text = _data.ToString();
        }

        IEnumerator ResaveData()
        {
            SelectPanel.Instance.Open();
            yield return new WaitUntil(() => { return SelectPanel.IsShow == false; });

            if (SelectPanel.CurrentData != null)
            {
                _data = SelectPanel.CurrentData;
                InitUI();
            }
            
            
        }
    }

    public List<Ball> resoult;
}
